package cn.school.controller.workflow;

import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.workflow.service.RepairApplyService;
import cn.school.workflow.vo.RepairApplyVO;
import cn.school.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/repairApply")
@Api(tags = "维修流程接口")
public class RepairApplyController {
    @Autowired
    private RepairApplyService repairApplyService;
    /**
     * 分页查询访客申请单
     * @return
     * @throws ApiException
     */
    @PostMapping("/findRepairApplyPageList")
    @ApiOperation("list => 分页查询维修申请单")
    public CommonResult<CommonPage<RepairApplyVO>> findRepairApplyPageList(@RequestBody RepairApplyVO vo) throws ApiException{
        IPage<RepairApplyVO> pageList =  repairApplyService.findRepairApplyPageList(vo);
        return  CommonResult.success(CommonPage.restPage(pageList));
    }

    @PostMapping("/cancelRepairApply")
    @ApiOperation("cancel => 撤销维修申请")
    public CommonResult<CommonPage<RepairApplyVO>> cancelRepairApply(@RequestBody WorkFlowVO vo) throws ApiException{
        boolean flag =  repairApplyService.cancelRepairApply(vo);
        if(flag){
            return CommonResult.success();
        }
        return  CommonResult.failed("流程取消失败");
    }

    @PostMapping("/createRepairApply")
    @ApiOperation("create => 创建维修流程")
    public CommonResult<Integer> createRepairApply(@RequestBody RepairApplyVO  vo) throws ApiException{

        int flag =  repairApplyService.createRepairApply(vo);
        if(flag>0){
            return CommonResult.success();
        }
        return  CommonResult.failed("流程创建失败");
    }

    @GetMapping("/findViolativeApplyDetail/{processInstanceId}")
    @ApiOperation("detail => 维修详情")
    public CommonResult<RepairApplyVO> findViolativeApplyDetail(@PathVariable("processInstanceId") Long processInstanceId) throws ApiException{
        RepairApplyVO repairApplyVO =  repairApplyService.findRepairApplyDetail(processInstanceId);
        if(repairApplyVO!=null){
            return CommonResult.success(repairApplyVO);
        }
        return  CommonResult.failed("查询不单申请单");
    }
}
